﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using RegisterationCommon.Notice;
using RegisterationCommon.Student;
using RegisterationCommon.Common;
using System.Data;
using RegisterationCommon.NoticeReceive;

namespace RegisterationSystem.Space_Student
{
    public partial class selnoice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (!StudentInfo.Current.IsLogin())
                {
                    Message.Send(10001);
                }
                bond();
            }
        }
        public void bond()
        {
            if (Request.QueryString["id"] != null)
            {
                int id = Convert.ToInt32(Request.QueryString["id"].ToString());
                int type = Convert.ToInt32(Request.QueryString["type"].ToString());
                if (type == 0)
                {
                    Notice n = new Notice();
                    if (n.GetHelpSubjectByID(id))
                    {
                        this.txttilt.InnerText = n.Tile;
                        this.txtcount.InnerText = n.Content;
                    }
                }
                else
                {
                    DataTable dtNotice = DBAction.GetDTFromSQL("select * from NoticeParents where ID=" + id);
                    if (dtNotice.Rows.Count > 0)
                    {
                        this.txttilt.InnerText = dtNotice.Rows[0]["Title"].ToString();
                        this.txtcount.InnerText = dtNotice.Rows[0]["Content"].ToString();
                        //弹出公告，代表此条信息被阅读
                        int nid =Convert.ToInt32(Request.QueryString["id"].ToString());
                        string num = CookiesInfo.GetValue("Robo", "IDCardNumber");
                        
                        DataTable stu = DBAction.GetDTFromSQL("select * from Students where IDCardNumber='" + num+"'");
                        if (stu.Rows.Count > 0)
                        {
                            int sid = Convert.ToInt32(stu.Rows[0]["ID"].ToString());//当前登录学生id
                            DataTable re = DBAction.GetDTFromSQL("select * from NoticeReceive where NoticeID=" + nid + "  and StudentID=" + sid);
                            if (re.Rows.Count > 0)//该公告中未找到这个信息则添加该学生信息
                            {
                                if (re.Rows[0]["Ext1"].ToString() == "0")//未阅读
                                {
                                    //更新信息
                                    RParams param = new RParams();
                                    string ext1 = "1";
                                    param.Add("Ext1", ext1);
                                    DBAction.Update("NoticeReceive", "Ext1", "NoticeID=" + nid + " and StudentID=" + sid, param);
                                }
                                else if (re.Rows[0]["Ext1"].ToString() == "1")
                                {
                                    //不用更新
                                }
                            }
                            else//添加已读信息，并进行标记已读
                            {
                                string where = "ID=" + sid;
                                DataTable dtst = DBAction.GetDTFromSQL("Select * From Students where " + where);//找到该学生
                                if (dtst.Rows.Count > 0)
                                {
                                    NoticeReceive rp = new NoticeReceive();
                                    rp.NoticeID = nid;
                                    rp.StudentID = sid;
                                    rp.ReceiveTime = DateTime.Now;
                                    rp.SchId = Convert.ToInt32(Cookies.Get("SchID"));
                                    rp.Sname = dtst.Rows[0]["TrueName"].ToString();
                                    rp.Gname = dtst.Rows[0]["FirstGuardianName"].ToString();
                                    rp.Address = dtst.Rows[0]["PermanentAddress"].ToString();
                                    rp.Ext1 = "1";//0为未查看公告
                                    rp.Ext2 = "";
                                    rp.Ext3 = "";
                                    rp.Ext4 = "";
                                    rp.AddNoticePt();
                                }
                            }

                        }
                        

                    }
                }
            }
        }
    }
}